﻿using Uniforax.Core.Common;
using Uniforax.Core.GameActions.GridMove;
using Uniforax.Core.GameObjects;

namespace Uniforax.Core.GameActions
{
    public class MoveStepGameObjectAction : BaseGameAction
    {
        // 起点的目标。
        public PosId FromPosId { get; set; }

        // 移动的目标。
        public PosId ToPosId { get; set; }

        //对象Id。
        public string ObjectId { get; set; }

        public MoveStepGameObjectAction(BaseGameObject gameObject, PosId fromPosId, PosId toPosId)
        {
            FromPosId = fromPosId;
            ToPosId = toPosId;
            ObjectId = gameObject.ObjectId;
        }

        public override void Execute(ActionContext context)
        {
            // 当单位离开格子后。
            var gridLeaveAction = new GridLeaveAction() { FromPosId = FromPosId,  ObjectId = ObjectId };
            context.History.Add(gridLeaveAction);
            gridLeaveAction.Execute(context);
            if (gridLeaveAction.IsCancenlled == true) { IsCancenlled = true; return; }

            // 当单位进入格子后。
            var gridEnterAction = new GridEnterAction() { ToPosId = ToPosId, ObjectId = ObjectId };
            context.History.Add(gridEnterAction);
            gridEnterAction.Execute(context);
            if (gridEnterAction.IsCancenlled == true) { IsCancenlled = true; return; }

            // 当单位正式放置到格子上时。
            var gridPlaceAction = new GridPlaceAction() { PlacePosId = ToPosId, ObjectId = ObjectId };
            context.History.Add(gridPlaceAction);
            gridPlaceAction.Execute(context);
            if (gridPlaceAction.IsCancenlled == true) { IsCancenlled = true; return; }
        }

        public override void Undo(ActionContext context)
        {
            // 因为是展开类型的，所以这里不会有任何处理。
        }

    }
}
